package com.uwlaser.modules.system.rest;

import com.uwlaser.annotation.Log;
import com.uwlaser.modules.system.domain.SysExternalHttpLog;
import com.uwlaser.modules.system.service.SysExternalHttpLogService;
import com.uwlaser.modules.system.service.dto.SysExternalHttpLogDto;
import com.uwlaser.modules.system.service.dto.SysExternalHttpLogQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.util.Set;

/**
* @author QZK
* @date 2022-12-07
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "HTTP请求日志管理")
@RequestMapping("/api/sysExternalHttpLog")
public class SysExternalHttpLogController {

    private final SysExternalHttpLogService sysExternalHttpLogService;

    @GetMapping
    @Log("查询HTTP请求日志")
    @ApiOperation("查询HTTP请求日志")
    @PreAuthorize("@el.check('sysExternalHttpLog:list')")
    public ResponseEntity query(SysExternalHttpLogQueryParam query, Pageable pageable){
        return new ResponseEntity<>(sysExternalHttpLogService.queryAll(query,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增HTTP请求日志")
    @ApiOperation("新增HTTP请求日志")
    @PreAuthorize("@el.check('sysExternalHttpLog:add')")
    public ResponseEntity create(@Validated @RequestBody SysExternalHttpLogDto resources){
        return new ResponseEntity<>(sysExternalHttpLogService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改HTTP请求日志")
    @ApiOperation("修改HTTP请求日志")
    @PreAuthorize("@el.check('sysExternalHttpLog:edit')")
    public ResponseEntity update(@Validated @RequestBody SysExternalHttpLogDto resources){
        sysExternalHttpLogService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除HTTP请求日志")
    @ApiOperation("删除HTTP请求日志")
    @PreAuthorize("@el.check('sysExternalHttpLog:del')")
    public ResponseEntity delete(@RequestBody Set<Integer> ids) {
        sysExternalHttpLogService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/reuploadOne")
    @Log("HTTP请求单条重传")
    @ApiOperation("HTTP请求单条重传")
    @PreAuthorize("@el.check('sysExternalHttpLog:edit')")
    public ResponseEntity reuploadOne(@RequestBody SysExternalHttpLog resources) throws Exception {
        sysExternalHttpLogService.reuploadOne(resources);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/reuploadAll")
    @Log("HTTP请求一键重传")
    @ApiOperation("HTTP请求一键重传")
    @PreAuthorize("@el.check('sysExternalHttpLog:edit')")
    public ResponseEntity reuploadAll(){
        sysExternalHttpLogService.reuploadAll();
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /*
    @Log("导出HTTP请求日志")
    @ApiOperation("导出HTTP请求日志")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('sysExternalHttpLog:list')")
    public void download(HttpServletResponse response, SysExternalHttpLogQueryParam query) throws IOException {
        sysExternalHttpLogService.download(sysExternalHttpLogService.queryAll(query), response);
    }*/

}
